<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>投票系统 - 找回密码</title>
    <!-- 引入Layui CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.8.17/css/layui.min.css">
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        /* 自定义样式 */
        body {
            background-color: #f8f9fa;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            box-sizing: border-box;
        }

        .login-container {
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
            padding: 40px;
            width: 100%;
            max-width: 400px;
            transition: all 0.3s ease;
        }

        .login-container:hover {
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.12);
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h2 {
            color: #333;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #666;
            font-size: 14px;
        }

        .layui-form-item {
            margin-bottom: 20px;
        }

        .layui-input {
            height: 44px;
            border-radius: 5px;
            padding-left: 40px;
            border-color: #e6e6e6;
        }

        .layui-input:focus {
            border-color: #1E9FFF !important;
            box-shadow: 0 0 0 2px rgba(30, 159, 255, 0.2);
        }

        .input-icon {
            position: absolute;
            left: 10px;
            top: 12px;
            color: #999;
            font-size: 18px;
        }

        .layui-btn {
            height: 44px;
            border-radius: 5px;
            background-color: #1E9FFF;
            font-size: 16px;
        }

        .layui-btn:hover {
            background-color: #0D8BD9;
        }

        .login-footer {
            margin-top: 20px;
            text-align: center;
            font-size: 14px;
        }

        .login-footer a {
            color: #1E9FFF;
        }

        .login-footer a:hover {
            text-decoration: underline;
        }

        /* 步骤指示器样式 */
        .step-indicator {
            display: flex;
            justify-content: space-between;
            margin-bottom: 30px;
            position: relative;
        }

        .step-indicator::before {
            content: "";
            position: absolute;
            top: 50%;
            left: 0;
            width: 100%;
            height: 2px;
            background-color: #e6e6e6;
            transform: translateY(-50%);
            z-index: 1;
        }

        .step {
            position: relative;
            z-index: 2;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background-color: #e6e6e6;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #999;
            font-weight: bold;
            transition: all 0.3s ease;
        }

        .step.active {
            background-color: #1E9FFF;
            color: white;
        }

        .step-text {
            position: absolute;
            top: 40px;
            left: 50%;
            transform: translateX(-50%);
            font-size: 12px;
            white-space: nowrap;
        }

        /* 响应式调整 */
        @media (max-width: 480px) {
            .login-container {
                padding: 30px 20px;
            }
        }

        /* 验证码倒计时按钮 */
        #getCodeBtn {
            position: absolute;
            right: 5px;
            top: 5px;
            height: 34px;
            line-height: 34px;
            padding: 0 10px;
            font-size: 12px;
            background-color: #1E9FFF;
            color: white;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        #getCodeBtn:hover {
            background-color: #0D8BD9;
        }

        #getCodeBtn.disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }

        /* 错误消息样式 */
        .error-message {
            color: red;
            margin-top: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="login-container">
    <div class="login-header">
        <h2><i class="fa fa-bar-chart" aria-hidden="true"></i> 投票系统</h2>
        <p>找回你的账号密码</p>
    </div>

    <!-- 步骤指示器 -->
    <div class="step-indicator">
        <div class="step active" id="step1">1</div>
        <div class="step" id="step2">2</div>
        <div class="step" id="step3">3</div>

        <div class="step-text" style="left: 15px;">验证邮箱</div>
        <div class="step-text" style="left: 50%;">输入验证码</div>
        <div class="step-text" style="right: 15px;">重置密码</div>
    </div>

    <!-- 错误消息显示区域 -->
    <div id="errorMessage" class="error-message" style="display: none;"></div>

    <!-- 步骤1：输入邮箱 -->
    <div id="step1Content">
        <form class="layui-form" id="emailForm">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="input-icon">
                        <i class="fa fa-envelope" aria-hidden="true"></i>
                    </div>
                    <input type="email" name="email" required lay-verify="required|email" placeholder="请输入注册邮箱"
                           autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <button type="button" id="sendCodeBtn" class="layui-btn layui-btn-fluid">
                    <i class="fa fa-paper-plane" aria-hidden="true"></i> 发送验证码
                </button>
            </div>
        </form>
    </div>

    <!-- 步骤2：输入验证码 -->
    <div id="step2Content" style="display: none;">
        <form class="layui-form" id="verifyForm">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="input-icon">
                        <i class="fa fa-key" aria-hidden="true"></i>
                    </div>
                    <input type="text" name="verificationCode" required lay-verify="required" placeholder="请输入验证码"
                           autocomplete="off" class="layui-input">
                    <div id="getCodeBtn">重新获取 (60)</div>
                </div>
            </div>

            <div class="layui-form-item">
                <button type="button" id="verifyBtn" class="layui-btn layui-btn-fluid">
                    <i class="fa fa-check" aria-hidden="true"></i> 验证并重置密码
                </button>
            </div>
        </form>
    </div>

    <!-- 步骤3：重置密码 -->
    <div id="step3Content" style="display: none;">
        <form class="layui-form" id="resetForm">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="input-icon">
                        <i class="fa fa-lock" aria-hidden="true"></i>
                    </div>
                    <input type="password" name="newPassword" required lay-verify="required|password"
                           placeholder="请设置新密码"
                           autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="input-icon">
                        <i class="fa fa-lock" aria-hidden="true"></i>
                    </div>
                    <input type="password" name="confirmPassword" required lay-verify="required|confirmPassword"
                           placeholder="请确认新密码"
                           autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <button type="button" id="resetBtn" class="layui-btn layui-btn-fluid">
                    <i class="fa fa-refresh" aria-hidden="true"></i> 确认重置密码
                </button>
            </div>
        </form>
    </div>

    <div class="login-footer">
        <a href="${pageContext.request.contextPath}/views/login.jsp"><i class="fa fa-arrow-left" aria-hidden="true"></i>
            返回登录页</a>
    </div>
</div>

<!-- 引入Layui JS -->
<script src="https://cdn.staticfile.org/layui/2.8.17/layui.min.js"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        // 保存当前步骤
        var currentStep = 1;
        // 模拟验证码
        var mockVerificationCode = '';

        // 自定义验证规则
        form.verify({
            password: function (value) {
                if (value.length < 6) {
                    return '密码长度不能小于6位';
                }
            },
            confirmPassword: function (value) {
                if (value !== $('input[name="newPassword"]').val()) {
                    return '两次输入的密码不一致';
                }
            }
        });

        // 验证码倒计时
        var countdown = 60;

        function startCountdown() {
            var btn = $('#getCodeBtn');
            btn.addClass('disabled');
            var timer = setInterval(function () {
                countdown--;
                btn.text('重新获取 (' + countdown + ')');
                if (countdown <= 0) {
                    clearInterval(timer);
                    btn.text('获取验证码');
                    btn.removeClass('disabled');
                    countdown = 60;
                }
            }, 1000);
        }

        // 显示错误消息
        function showErrorMessage(message) {
            $('#errorMessage').text(message).show();
            setTimeout(function () {
                $('#errorMessage').fadeOut();
            }, 3000);
        }

        // 模拟生成验证码
        function generateVerificationCode(email) {
            let code;
            $.ajax({
                url: '${pageContext.request.contextPath}/fw/sendEmail',
                type: 'POST',
                async: false,
                data: {
                    email: email
                },
                success: function (data) {
                    code = data
                    console.log(data)
                    layer.msg('验证码已发送到邮箱，请不要离开此页面', {icon: 1})
                }, error: function (data) {
                    layer.msg('验证码发送失败', {icon: 5})
                }
            })


            return code;
        }

        // 获取验证码按钮点击事件
        $('#getCodeBtn').on('click', function () {
            if (!$(this).hasClass('disabled')) {
                sendVerificationCode();
            }
        });

        // 发送验证码
        function sendVerificationCode() {
            // 验证邮箱格式
            var email = $('input[name="email"]').val();
            if (!email) {
                showErrorMessage('请输入邮箱地址');
                return;
            }

            var emailReg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
            if (!emailReg.test(email)) {
                showErrorMessage('请输入有效的邮箱地址');
                return;
            }

            // 模拟发送验证码
            mockVerificationCode = generateVerificationCode(email);
            console.log('模拟验证码:', mockVerificationCode); // 仅用于调试
            startCountdown();
            // 切换到步骤2
            currentStep = 2;
            $('#step1').removeClass('active');
            $('#step2').addClass('active');
            $('#step1Content').hide();
            $('#step2Content').show();
        }

        // 发送验证码按钮点击事件
        $('#sendCodeBtn').on('click', function () {
            sendVerificationCode();
        });

        // 验证验证码并切换到重置密码页面
        $('#verifyBtn').on('click', function () {
            var verificationCode = $('input[name="verificationCode"]').val();
            if (!verificationCode) {
                showErrorMessage('请输入验证码');
                return;
            }

            // 模拟验证验证码
            if (verificationCode === mockVerificationCode) {
                layer.msg('验证码验证通过', {icon: 1});

                // 切换到步骤3
                currentStep = 3;
                $('#step2').removeClass('active');
                $('#step3').addClass('active');
                $('#step2Content').hide();
                $('#step3Content').show();
            } else {
                showErrorMessage('验证码错误，请重新输入');
            }
        });

        // 重置密码
        $('#resetBtn').on('click', function () {
            var newPassword = $('input[name="newPassword"]').val();
            var confirmPassword = $('input[name="confirmPassword"]').val();

            if (!newPassword || !confirmPassword) {
                showErrorMessage('请输入密码');
                return;
            }

            if (newPassword !== confirmPassword) {
                showErrorMessage('两次输入的密码不一致');
                return;
            }

            if (newPassword.length < 6) {
                showErrorMessage('密码长度不能小于6位');
                return;
            }


            var email = $('input[name="email"]').val();
            $.ajax({
                url: '${pageContext.request.contextPath}/fw/fd',
                type: 'post',
                data: {
                    email: email,
                    password: newPassword
                }, success: function (res) {
                    console.log("res", res)
                    if (res === "success") {
                        layer.msg('密码重置成功，请重新登录', {icon: 1})
                        setTimeout(function () {
                            window.location.href = '${pageContext.request.contextPath}/views/login.jsp';
                        }, 1000);
                    } else {
                        layer.msg('密码重置失败', {icon: 5})
                    }
                }, error: function (res) {
                    layer.msg('密码重置失败', {icon: 5})
                }
            });
        });
    });
</script>
</body>
</html>